import base64 from './code'; 
import dayjs from 'dayjs';
// import bus from './bus';

//鉴权安全令牌
const TOKEN = "auth_token";
//用户信息
const USER = "UserInfo";
//菜单信息
const MENU = "AppMenus";
//用户登出
const LOGOUT = "LogoutUser";

//缓存注销登录人姓名
export function cacheLogoutUser(name) {
  localStorage.setItem(LOGOUT, name);
}

//检查提示注销登录人姓名
export function checkLogoutUser(v) {
  let logout = localStorage.getItem(LOGOUT);
  if (logout) {
    v.$notify({
      title: "提示",
      message: logout + " 注销登录成功!",
      type: "success",
    });
    localStorage.removeItem(LOGOUT);
  }
}

//清空缓存
export function clearCache() {
  localStorage.clear();
  sessionStorage.clear();
}
//获取缓存的用户信息
export function getTabIndex() {
  if(sessionStorage.getItem('tabIndex')){
    return JSON.parse(base64.decode(sessionStorage.getItem('tabIndex')));
  }else{
    return '1'
  }
}

//缓存用户信息
export function setTabIndex(tabIndexValue) {
  sessionStorage.setItem('tabIndex', base64.encode(JSON.stringify(tabIndexValue)));
}

//获取缓存的用户信息
export function getUserInfo() {
  return JSON.parse(base64.decode(localStorage.getItem(USER)));
}

//缓存用户信息
export function setUserInfo(info) {
  localStorage.setItem(USER, base64.encode(JSON.stringify(info)));
}

//获取用户菜单
export function getMenus() {
  return JSON.parse(base64.decode(localStorage.getItem(MENU)));
}

//缓存用户菜单
export function setMenus(menus) {
  localStorage.setItem(MENU, base64.encode(JSON.stringify(menus)));
}
//由路由路径搜索菜单名称
export function getMenuMeta(path, menus = null) {
  let meta = null;
  if (menus == null) menus = getMenus();
  for (let menu of menus) {
    if (menu.hrefLink == path || menu.hrefLink + "/" + menu.printLink == path) {
      meta = {
        title: menu.menuName,
        context: menu.remark,
        print: menu.printName,
        compare: path,
      };
      break;
    }
    if (menu.child && menu.child.length > 0) {
      meta = getMenuMeta(path, menu.child);
      if (meta != null) break;
    }
  }
  return meta;
}

//获取登录TOKEN令牌
export function getToken() {
  return localStorage.getItem(TOKEN);
  // return sessionStorage.getItem(TOKEN);
}

//缓存登录TOKEN令牌
export function setToken(token) {
  localStorage.setItem(TOKEN, token);
  // sessionStorage.setItem(TOKEN, token);
}

//重置表单校验
export function resetDialog(v, name) {
  setTimeout(() => {
    v.$refs[name].clearValidate();
  }, 200);
}

//校验是否为空
export function isNull(value) {
  return value == undefined || value == null || value == "";
}

//校验是否为空
export function isZeroList(value) {
  return value == undefined || value == null || value.length <= 0;
}

//格式化时间
export function formatDate(date, format = "YYYY-MM-DD HH:mm:ss") {
  return dayjs(date).format(format);
}

//获取DOM属性
export function getStyle(obj, att) {
  if (obj.currentStyle) {
    //IE
    return obj.currentStyle[att];
  } else {
    //Chrom  firefox
    return window.getComputedStyle(obj, false)[att];
  }
}

//判空
export function isEmpty(v) {
  try {
    if (v) {
      if (typeof v === "object" && Object.keys(v).length === 0) return true;
      return false;
    }
    return true;
  } catch (e) {
    return true;
  }
}

//求两个数组的差集
export function diffArray(arr1, arr2) {
  return arr1.filter(function (val) {
    return arr2.indexOf(val) === -1;
  });
}

// 点击防抖
const throttle = {
  bind: (el, binding) => {
    let throttleTime = binding.value // 防抖时间
    if (!throttleTime) { // 用户若不设置防抖时间，则默认1s
      throttleTime = 1000
    }
    let timer
    let disable = false
    el.addEventListener('click', event => {
      if (timer) {
        clearTimeout(timer)
      }
      if (!disable) { // 第一次执行(一点击触发当前事件)
        disable = true
      } else {
        event && event.stopImmediatePropagation()
      }
      timer = setTimeout(() => {
        timer = null
        disable = false
      }, throttleTime)
    }, true)
  }
}
export default throttle
